﻿Public Class clsInhSwiadectwo
    Inherits clsSwiadectwo

#Region "Attributes"

    Private mvarWlasciwosciMechaniczne As Collection(Of clsInhWlasciwosciMechaniczne)
    Private mvarWlasciwosciMechaniczneDel As Collection(Of clsInhWlasciwosciMechaniczne)

    Private mvarKlient As clsKlient
    Private mvarGatunek As clsInhGatunek
    Private mvarPakowanie As clsPakowanie

    Private mvarSkladChemiczny As Collection(Of clsInhSwiadectwoSkladChemiczny)
    Private mvarSkladChemicznyDel As Collection(Of clsInhSwiadectwoSkladChemiczny)

#End Region

#Region "Properties"

    Public Property WlasciwosciMechaniczne() As Collection(Of clsInhWlasciwosciMechaniczne)
        Get
            Return mvarWlasciwosciMechaniczne
        End Get
        Set(ByVal value As Collection(Of clsInhWlasciwosciMechaniczne))
            mvarWlasciwosciMechaniczne = value
        End Set
    End Property

    Public Property WlasciwosciMechaniczneDel() As Collection(Of clsInhWlasciwosciMechaniczne)
        Get
            Return mvarWlasciwosciMechaniczneDel
        End Get
        Set(ByVal value As Collection(Of clsInhWlasciwosciMechaniczne))
            mvarWlasciwosciMechaniczneDel = value
        End Set
    End Property

    Public Property Klient As clsKlient
        Get
            Return mvarKlient
        End Get
        Set(ByVal value As clsKlient)
            mvarKlient = value
        End Set
    End Property

    Public Property Gatunek As clsInhGatunek
        Get
            Return mvarGatunek
        End Get
        Set(ByVal value As clsInhGatunek)
            mvarGatunek = value
        End Set
    End Property

    Public Property Pakowanie As clsPakowanie
        Get
            Return mvarPakowanie
        End Get
        Set(ByVal value As clsPakowanie)
            mvarPakowanie = value
        End Set
    End Property

    Public Property SkladChemicznyDel() As Collection(Of clsInhSwiadectwoSkladChemiczny)
        Get
            Return mvarSkladChemicznyDel
        End Get
        Set(ByVal value As Collection(Of clsInhSwiadectwoSkladChemiczny))
            mvarSkladChemicznyDel = value
        End Set
    End Property

    Public Property SkladChemiczny As Collection(Of clsInhSwiadectwoSkladChemiczny)
        Get
            Return mvarSkladChemiczny
        End Get
        Set(ByVal value As Collection(Of clsInhSwiadectwoSkladChemiczny))
            mvarSkladChemiczny = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()
        mvarWlasciwosciMechaniczne = New Collection(Of clsInhWlasciwosciMechaniczne)
        mvarWlasciwosciMechaniczneDel = New Collection(Of clsInhWlasciwosciMechaniczne)
        mvarSkladChemiczny = New Collection(Of clsInhSwiadectwoSkladChemiczny)
        mvarSkladChemicznyDel = New Collection(Of clsInhSwiadectwoSkladChemiczny)
        mvarKlient = New clsKlient
        mvarGatunek = New clsInhGatunek
        mvarPakowanie = New clsPakowanie

    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase)

        MyBase.New(ID.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)
        mvarWlasciwosciMechaniczne = New Collection(Of clsInhWlasciwosciMechaniczne)
        mvarWlasciwosciMechaniczneDel = New Collection(Of clsInhWlasciwosciMechaniczne)

        mvarKlient = New clsKlient(MyBase.KlientID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarGatunek = New clsInhGatunek(MyBase.GatunekId, lvarDbHolder)
        mvarPakowanie = New clsPakowanie(MyBase.PakowanieId.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)
      
        Dim lvarTabel As DataTable = PobierzWlasciwosciMechaniczne(lvarDbHolder, ID)

        If Not lvarTabel Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel.Rows
                mvarWlasciwosciMechaniczne.Add(New clsInhWlasciwosciMechaniczne(CInt(lvarRow.Item("id")), lvarDbHolder))
            Next
        End If

        mvarSkladChemiczny = New Collection(Of clsInhSwiadectwoSkladChemiczny)
        mvarSkladChemicznyDel = New Collection(Of clsInhSwiadectwoSkladChemiczny)

        Dim lvarTabel2 As DataTable = PobierzSkladChemiczny(lvarDbHolder, ID)

        If Not lvarTabel2 Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel2.Rows
                mvarSkladChemiczny.Add(New clsInhSwiadectwoSkladChemiczny(CInt(lvarRow.Item("id")), lvarDbHolder))
            Next
        End If

    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)

        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarWlasciwosc As clsInhWlasciwosciMechaniczne In mvarWlasciwosciMechaniczneDel
            lvarWlasciwosc.Delete(lvarDbHolder)
        Next

        For Each lvarWlasciwosc As clsInhWlasciwosciMechaniczne In mvarWlasciwosciMechaniczne
            lvarWlasciwosc.SwiadectwoId = MyBase.Id.ToString()
            lvarWlasciwosc.Save(lvarDbHolder)
        Next

        For Each lvarSklad As clsInhSwiadectwoSkladChemiczny In mvarSkladChemicznyDel
            lvarSklad.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
        Next

        For Each lvarSklad As clsInhSwiadectwoSkladChemiczny In mvarSkladChemiczny
            lvarSklad.SwiadectwoId = MyBase.Id
            lvarSklad.Save(lvarDbHolder)
        Next

    End Sub

    Shared Function PobierzSkladChemiczny(lvarDbHolder As clsDataBase, Id As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable = Nothing

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_swiadectwo", 1, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_id", Id.ToString())
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

    Shared Function PobierzWlasciwosciMechaniczne(lvarDbHolder As clsDataBase, Id As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable = Nothing

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_swiadectwo", 2, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_id", Id.ToString())
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function


#End Region

End Class
